Model View Control Issue: Null Pointer Initialization Question [closed]
Posted
by
David Dimalanta
on Programmers
See other posts from Programmers
or by David Dimalanta
Published on 2012-08-28T02:03:58Z
Indexed on
2012/08/28
3:51 UTC
Read the original article
Hit count: 302
Good morning again. This is David. Please, I need an urgent help regarding control model view where I making a code that uniquely separating into groups:
- An Activity Java Class to Display the Interface
- A View and Function Java Class for Drawing Cards and Display it on the Activity Class
The problem is that the result returns a Null Pointer Exception. I have initialize for the ID for Text View and Image View. Under this class "draw_deck.java". Please help me.
Here's my code for draw_deck.java:
package com.bodapps.inbetween.model;
import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import com.bodapps.inbetween.R;
public class draw_deck extends View
{
public TextView count_label;
public ImageView draw_card;
private int count;
public draw_deck(Context context)
{
super(context);
// TODO Auto-generated constructor stub
//I have initialized two widgets for ID. I still don't get it why I got forced closed by Null Pointer Exception thing.
draw_card = (ImageView) findViewById(R.id.IV_Draw_Card);
count_label = (TextView) findViewById(R.id.Text_View_Count_Card);
}
public void draw(int s, int c, String strSuit, String strValue, Pile pile, Context context)
{
//super(context);
//Just printing the card drawn from pile
int suit, value = 1;
draw_card = (ImageView) findViewById(R.id.IV_Draw_Card);
count_label = (TextView) findViewById(R.id.Text_View_Count_Card);
Card card;
if(!pile.isEmpty()) //Setting it to IF statement displays the card one by one.
{
card = pile.drawFromPile();
//Need to check first if card is null.
if (card != null)
{
//draws an extra
if (card != null)
{
//Get suit of card to print out.
suit = card.getSuit();
switch (suit)
{
case CardInfo.DIAMOND:
strSuit = "DIAMOND";
s=0;
break;
case CardInfo.HEART:
strSuit = "HEART";
s=1;
break;
case CardInfo.SPADE:
strSuit = "SPADE";
s=2;
break;
case CardInfo.CLUB:
strSuit = "CLUB";
s=3;
break;
}
//Get value of card to print out.
value = card.getValue();
switch (value)
{
case CardInfo.ACE:
strValue = "ACE";
c=0;
break;
case CardInfo.TWO:
c=1;
break;
case CardInfo.THREE:
strValue = "THREE";
c=2;
break;
case CardInfo.FOUR:
strValue = "FOUR";
c=3;
break;
case CardInfo.FIVE:
strValue = "FIVE";
c=4;
break;
case CardInfo.SIX:
strValue = "SIX";
c=4;
break;
case CardInfo.SEVEN:
strValue = "SEVEN";
c=4;
break;
case CardInfo.EIGHT:
strValue = "EIGHT";
c=4;
break;
case CardInfo.NINE:
strValue = "NINE";
c=4;
break;
case CardInfo.TEN:
strValue = "TEN";
c=4;
break;
case CardInfo.JACK:
strValue = "JACK";
c=4;
break;
case CardInfo.QUEEN:
strValue = "QUEEN";
c=4;
break;
case CardInfo.KING:
strValue = "KING";
c=4;
break;
}
}
}
}//
//Below two lines of code, this is where issued the Null Pointer Exception.
draw_card.setImageResource(deck[s][c]);
count_label.setText(new StringBuilder(strValue).append(" of ").append(strSuit).append(String.valueOf(" " + count++)).toString());
}
//Choice of Suits in a Deck
public Integer[][] deck =
{
//Array Group 1 is [0][0] (No. of Cards: 4 - DIAMOND)
{
R.drawable.card_dummy_1,
R.drawable.card_dummy_2,
R.drawable.card_dummy_4,
R.drawable.card_dummy_5,
R.drawable.card_dummy_3
},
//Array Group 2 is [1][0] (No. of Cards: 4 - HEART)
{
R.drawable.card_dummy_1,
R.drawable.card_dummy_2,
R.drawable.card_dummy_4,
R.drawable.card_dummy_5,
R.drawable.card_dummy_3
},
//Array Group 3 is [2][0] (No. of Cards: 4 - SPADE)
{
R.drawable.card_dummy_1,
R.drawable.card_dummy_2,
R.drawable.card_dummy_4,
R.drawable.card_dummy_5,
R.drawable.card_dummy_3
},
//Array Group 4 is [3][0] (No. of Cards: 4 - CLUB)
{
R.drawable.card_dummy_1,
R.drawable.card_dummy_2,
R.drawable.card_dummy_4,
R.drawable.card_dummy_5,
R.drawable.card_dummy_3
},
};
}
And this one of the activity class, Player_Mode_2.java:
package com.bodapps.inbetween;
import java.util.Random;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.bodapps.inbetween.model.Card;
import com.bodapps.inbetween.model.Pile;
import com.bodapps.inbetween.model.draw_deck;
/*
*
* Public class for Two-Player mode.
*
*/
public class Player_Mode_2 extends Activity
{
//Image Views
private ImageView draw_card;
private ImageView player_1;
private ImageView player_2;
private ImageView icon;
//Buttons
private Button set_deck;
//Edit Texts
private EditText enter_no_of_decks;
//text Views
private TextView count_label;
//Integer Data Types
private int no_of_cards, count;
private int card_multiplier;
//Contexts
final Context context = this;
//Pile Model
public Pile pile;
//Card Model
public Card card;
//create View
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.play_2_player_mode);
//-----[ Search for Views ]-----
//Initialize for Image View
draw_card = (ImageView) findViewById(R.id.IV_Draw_Card);
player_1 = (ImageView) findViewById(R.id.IV_Player_1_Card);
player_2 = (ImageView) findViewById(R.id.IV_Player_2_Card);
//Initialize for Text view or Label
count_label = (TextView) findViewById(R.id.Text_View_Count_Card);
//-----[ Adding Values ]-----
//Integer Values
count = 0;
no_of_cards = 0;
//-----[ Adding Dialog ]-----
//Initializing Dialog
final Dialog deck_dialog = new Dialog(context);
deck_dialog.setContentView(R.layout.dialog);
deck_dialog.setTitle("Deck Dialog");
//-----[ Initializing Views for Dialog's Contents ]-----
//Initialize for Edit Text
enter_no_of_decks = (EditText) deck_dialog.findViewById(R.id.Edit_Text_Set_Number_of_Decks);
//Initialize for Button
set_deck = (Button) deck_dialog.findViewById(R.id.Button_Deck);
//-----[ Setting onClickListener() ]-----
//Set Event Listener for Image view
draw_card.setOnClickListener(new Draw_Card_Model());
//Set Event Listener for Setting the Deck
set_deck.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
if(card_multiplier <= 8)
{
//Use "Integer.parseInt()" method to instantly convert from String to int value.
card_multiplier = Integer.parseInt(enter_no_of_decks.getText().toString());
//Shuffling cards...
pile = new Pile(card_multiplier); //Multiply no. of decks
//Dismiss or close the dialog.
deck_dialog.dismiss();
}
else
{
Toast.makeText(getApplicationContext(), "Please choose a number from 1 to 8.", Toast.LENGTH_SHORT).show();
}
}
});
//Show dialog.
deck_dialog.show();
}
//Shuffling the Array
public void Shuffle_Cards(Integer[][] Shuffle_Deck)
{
Random random = new Random();
for(int i = Shuffle_Deck[no_of_cards].length - 1; i >=0; i--)
{
int Index = random.nextInt(i + 1);
//Simple Swapping
Integer swap = Shuffle_Deck[card_multiplier-1][Index];
Shuffle_Deck[card_multiplier-1][Index] = Shuffle_Deck[card_multiplier-1][i];
Shuffle_Deck[card_multiplier-1][i] = swap;
}
}
//Private Class for Random Card Draw
private class Draw_Card_Model implements OnClickListener
{
public void onClick(View v)
{
//Just printing the card drawn from pile
int suit = 0, value = 0;
String strSuit = "", strValue = "";
draw_deck draw = new draw_deck(context); //This line is where issued the Null Pointer Exception.
if (count == card_multiplier*52)
{
// A message shows up when all cards are draw out.
Toast.makeText(getApplicationContext(), "All cards have been used up.", Toast.LENGTH_SHORT).show();
draw_card.setEnabled(false);
}
else
{
draw.draw(suit, value, strSuit, strValue, pile, context);
count_label.setText(count); //This is where I got force closed error, although "int count" have initialized the number. This was supposed to accept in the setText() method.
count++;
}
}
}
}
Take note that the issues on Null Pointer Exception is the Image View and the Edit Text. I got to test it. Thanks. If you have any info about my question, let me know it frankly.
© Programmers or respective owner